<?php
/**
 * Pertyler DatabaseManagement
 * DatabaseModel
 * Table Abstract Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

abstract class aTable
{
	protected static $object;

	protected $parent_id;

	protected $id;

	protected $name;

	protected $shortName;

	protected $backgroundName;

	private $schema;

	protected $hasNameTable;

	protected $hasNameTableAlternativeColumn;

	protected $hasLanguage;

	protected $tableType;

	private $hasUnpublishedColumn;

	protected function __construct($parent_id,
			$id,
			$name,
			$shortName,
			$backgroundName,
			$schema,
			$hasNameTable,
			$hasNameTableAlternativeColumn,
			$hasLanguage,
			$tableType,
			$hasUnpublishedColumn)
	{
		$this->parent_id = $parent_id;

		$this->id = $id;

		$this->name = $name;

		$this->shortName = $shortName;

		$this->backgroundName = $backgroundName;

		$this->schema = $schema;

		$this->hasNameTable = $hasNameTable;

		$this->hasNameTableAlternativeColumn = $hasNameTableAlternativeColumn;

		$this->hasLanguage = $hasLanguage;

		$this->tableType = $tableType;

		$this->hasUnpublishedColumn = $hasUnpublishedColumn;

		self::$object[$id] = $this;
	}

	final public function Id()
	{
		return $this->id;
	}

	public function __ToString()
	{
		return $this->name;
	}

	final public function BackgroundName()
	{
		return $this->backgroundName;
	}

	final public function Schema()
	{
		return $this->schema;
	}

	final public function HasNameTable()
	{
		return $this->hasNameTable;
	}

	final public function HasNameTableAlternativeColumn()
	{
		return $this->hasNameTableAlternativeColumn;
	}

	final public function ConnectorTable()
	{
		$nameArray = explode('_',
				$this->backgroundName);

		$id = 0;

		while((isset($nameArray[$id + 1])))
			$id++;

		if(ucfirst($nameArray[$id]) == $nameArray[$id])
			return false;

		if($nameArray[$id] == 'name')
			return false;

		return true;
	}

	final public function NameTable()
	{
		$nameArray = explode('_',
				$this->backgroundName);

		$id = 0;

		while((isset($nameArray[$id + 1])))
			$id++;

		if($nameArray[$id] == 'name')
			return true;

		return false;
	}

	final protected function baseName()
	{
		$nameArray = explode('_',
				$this->backgroundName);

		$name = '';

		$id = 0;

		while(((isset($nameArray[$id]))) AND (!is_numeric($nameArray[$id])))
		{
			if($id)
			$name .= '_';

			$name .= $nameArray[$id];

			$id++;
		}

		return $name;
	}

	final public function UniqueName()
	{
		$nameArray = explode('_',
				$this->backgroundName);

		$name = '';

		$id = 0;

		while(((isset($nameArray[$id]))) AND (!is_numeric($nameArray[$id])))
		{
			if(ucfirst($nameArray[$id]) == $nameArray[$id])
				$name = $nameArray[$id];
			else
				$name .= ucfirst($nameArray[$id]);

			$id++;
		}

		return $name;
	}

	final public function TableType()
	{
		return $this->tableType;
	}

	final public function FileTable()
	{
		if($this->tableType == 'file')
			return true;

		if($this->tableType == 'imageFile')
			return true;

		if($this->tableType == 'documentFile')
			return true;

		return false;
	}

	final public function HasUnpublishedColumn()
	{
		return $this->hasUnpublishedColumn;
	}

	final public function RowsTable(Table_Row $row)
	{
		require_once __DIR__ . '/PublishedRow.php';

		return new PublishedRow_Table($row,
				$this->parent_id,
				$this->id,
				$this->name,
				$this->shortName,
				$this->backgroundName,
				$this->schema,
				$this->hasNameTable,
				$this->hasNameTableAlternativeColumn,
				$this->hasLanguage,
				$this->tableType,
				$this->hasUnpublishedColumn);
	}
}
